package com.plant.serviceImpl;

import com.plant.dao.BaseDao;
import com.plant.entity.UgcFriends;
import com.plant.entity.UgcFriendsApply;
import com.plant.service.Friendservic;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author 植物先生-李琦
 */
@Service                                                                        //依赖注册 必须写注册 下面在写resource资源
@Transactional
public class FriendserviceImpl implements Friendservic{
    @Resource(name = "baseDaoImpl")      //注解扫描basedao接口方法
    private BaseDao baseDao;
    

	public boolean addFriends(UgcFriendsApply apply) {							//--1.添加好友请求到好友列表的方法
        boolean b=baseDao.add(apply); 
    	return b;        
	}
	
    public boolean update(UgcFriendsApply apply) {//---同意加好友   修改state=1
    	boolean b=true;
    	String hql="update UgcFriendsApply set state=1 where userId=("+apply.getUserId()+") and toUserId=("+apply.getToUserId()+")";
    	baseDao.updatehql(hql);
    	String hql1="update UgcFriendsApply set state=1 where userId=("+apply.getToUserId()+") and toUserId=("+apply.getUserId()+")";
    	baseDao.updatehql(hql1);
    	return b;
    }
    
    public boolean Ignoreupdate(UgcFriendsApply apply) {//---忽略加好友  修改state=2
    	String hql="update UgcFriendsApply set state=2 where id=("+apply.getId()+")";
    	return baseDao.updatehql(hql);
    }
    
    public boolean addfriend(UgcFriends uf) {   //---3.将同意的好友 添加到正式朋友表中
        boolean b=baseDao.add(uf);
        return b;
    }

    @SuppressWarnings("unchecked")
	public List<Object> listfriend(Object obj) { //---4.查找显示  我的好友
      String hql="select B.id,B.nickname,B.photo from ugc_user as B where B.id in("+obj+")";
      return baseDao.lianhe(hql, null);
    }
    @SuppressWarnings("unchecked")
    public List<Object> listnotfriend(Object obj) { //---查看不是好友信息
    	String hql="select B.id,B.nickname,B.photo user_photo from ugc_user as B where B.id not in("+obj+")";
    	return baseDao.lianhe(hql, null);
    }
    

	public Object one(UgcFriendsApply apply) {//----查找单个好友请求详情					
		return baseDao.find(UgcFriendsApply.class, apply.getId());
	}

	
	public boolean delfriend(UgcFriends uf) {//----删除我的好友
		boolean b=true;
		String hql="delete from UgcFriends where receiveUserId=("+uf.getReceiveUserId()+")and applyUserId=("+uf.getApplyUserId()+") or applyUserId=("+uf.getReceiveUserId()+")and receiveUserId=("+uf.getApplyUserId()+")";
		b=baseDao.updatehql(hql);
		return b;
	}

	public boolean UPState(UgcFriends uf) {//-----修改删除 修改用户状态为state=3
		String hql="update UgcFriendsApply set state=3 where userId=("+uf.getApplyUserId()+") and toUserId=("+uf.getReceiveUserId()+") or userId=("+uf.getReceiveUserId()+") and toUserId=("+uf.getApplyUserId()+")";
		return baseDao.updatehql(hql);
	}

	public boolean ValidateFriend(UgcFriendsApply apply) {
		String hql="from UgcFriends where receiveUserId =:receiveUserId and applyUserId =:applyUserId";
		List<?> list=baseDao.findname(hql, new String[]{"receiveUserId","applyUserId"}, new Object[]{apply.getUserId(),apply.getToUserId()});
		String hql1="from UgcFriends where receiveUserId =:applyUserId and applyUserId =:receiveUserId";
		List<?> list2=baseDao.findname(hql1, new String[]{"applyUserId","receiveUserId"}, new Object[]{apply.getUserId(),apply.getToUserId()});
		UgcFriends u = null;
		for (int i = 0; i < list.size(); i++) {
			u=(UgcFriends) list.get(0);
		}
		for (int i = 0; i < list2.size(); i++) {
			u=(UgcFriends) list.get(0);
		}
		boolean b = false;
		if(u==null){
			b=false;
			return b;	
		}else{
			b=true;
			return b;
		}
	}
	
	
}
